/**
 * 
 */
package com.es.controller;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.richfaces.component.html.HtmlDataTable;

import com.es.bo.LeadQualificationPropertyValueBO;
import com.es.bo.ValueBO;
import com.es.business.businessRule.IBusinessRuleBL;
import com.es.business.lead.ILeadManagement;
import com.es.exception.business.ESException;
import com.es.model.Lead;
import com.es.model.LeadQualification;
import com.es.util.ProjectConstant;




/**
 * @author Bushra Nazir
 *
 */
public class LeadConversionController extends BaseController{

	private static final Logger log = Logger.getLogger(LeadConversionController.class);


	private String tempVarCLQ;

	private ILeadManagement leadManagementImpl;
	private IBusinessRuleBL businessRuleImpl;

	private List <LeadQualificationPropertyValueBO> leadQualificationPropertyValueBOList;	



	private HtmlDataTable leadQualificationBODataTable;
	private HtmlDataTable radioValueDataTable;

	private ValueBO valueBO;
	private LeadQualification leadQualification;
	private Lead lead;

	private Boolean saveButtonIsDisabled;

	//private Integer selectedLeadIdToConvert;
	private Lead selectedLeadToConvert;

	private String navigationIdentifier;

	//private Integer leadId;





	public void initForLeadConversion(){

		log.info("inside initForLeadConversion()" );
		
		FacesContext fc = FacesContext.getCurrentInstance();

		selectedLeadToConvert = (Lead) fc.getExternalContext().getRequestMap().get(ProjectConstant.LEAD_TO_BE_CONVERTED_SESSION_KEY);
		
		log.info(selectedLeadToConvert.getFirstname());
		log.info(selectedLeadToConvert.getLeadId());

	}


	/*public String navigateToConvertLeadToOpportunity(){

		log.info("inside navigateToConvertLeadToOpportunity() ");

		//selectedLeadIdToConvert = Integer.parseInt(getParamValueFromRequest(ProjectConstant.SELECTED_LEADID_TO_CONVERT_PARAM_NAME));
		//setSelectedLeadIdToConvert( selectedLeadIdToConvert );


		selectedLeadIdToConvert = Integer.parseInt(getParamValueFromRequest("leadId"));
		setSelectedLeadIdToConvert( selectedLeadIdToConvert );

		navigationIdentifier= ProjectConstant.LEAD_QUALIFICATION_QUERIES_NAVIGATION_IDENTIFIER;
		return navigationIdentifier;

	}*/


	public void generateUniqueLeadQualificationQuestions(){

		saveButtonIsDisabled = false;

	//	HttpServletRequest request = getRequestFromFacesContext();
	
		try {

			String str = getMessageResourceString(ProjectConstant.APPLICATION_BUSINESS_RULE_RESOURCES_PATH, ProjectConstant.BUSINESS_RULE_1_KEY, null);
			Integer businessRuleId = Integer.parseInt(str);
			leadQualificationPropertyValueBOList =  businessRuleImpl.getUniqueLeadQualificationQuestions(selectedLeadToConvert,businessRuleId);

		} catch (ESException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		log.info("Length of queries retrived from DB : " + leadQualificationPropertyValueBOList.size());

		if(leadQualificationPropertyValueBOList.size()== 0){

			log.info("no queries to be answered.....");

			FacesContext context = FacesContext.getCurrentInstance();		

			String noQuestionMsg ="  " + getMessageResourceString(ProjectConstant.APPLICATION_MESSAGE_RESOURCES_PATH, "NO_QUESTIONS_MSG", null) ;
			addGlobalFacesMessage(context, noQuestionMsg, null,FacesMessage.SEVERITY_ERROR);

			saveButtonIsDisabled = true;
		} else{

			saveButtonIsDisabled = false;
		}
	}


	public Lead getSelectedLeadById(Integer selectedLeadIdToView) {

		log.info(" inside getSelectedLeadById() ");
		Lead selectedLead = null;

		try {
			selectedLead = leadManagementImpl.getLead(selectedLeadIdToView);

		} catch (ESException e) {

			String errMsg = getMessageResourceString(ProjectConstant.APPLICATION_MESSAGE_RESOURCES_PATH, "LEAD_RETRIEVAL_FAILED_MSG",null);
			FacesContext fc = FacesContext.getCurrentInstance();
			addGlobalFacesMessage(fc, errMsg, null, FacesMessage.SEVERITY_ERROR);
		}

		return selectedLead;

	}




	public  String  setSelected() {

		log.info("------------------>>>> setSelected called");

		valueBO =  (ValueBO) radioValueDataTable.getRowData();
		leadQualification = new LeadQualification();
		
		if(selectedLeadToConvert!=null){
			
		log.info("ValueId from Rowdata()="+valueBO.getValueId());
	
		leadQualification.setValueId(valueBO.getValueId());		
		leadQualification.setLeadId(selectedLeadToConvert.getLeadId());		
		leadQualification.setPropertyId(valueBO.getPropertyId()); // to match with BusinessRule.properties entries laters during checking qualification
		
		}


		int rowIndexR = radioValueDataTable.getRowIndex();
		log.info( "row index :: " + rowIndexR );


		leadQualificationPropertyValueBOList.get(leadQualificationBODataTable.getRowIndex()).getLeadQualificationList().add(leadQualification);
		return "do nothing";

	}



	public String submitValues(){

		String navigationIdentifier = null;


		if(selectedLeadToConvert!=null) {

			try {
				businessRuleImpl.saveLeadQualificationValues(leadQualificationPropertyValueBOList, selectedLeadToConvert);
			} catch (ESException e) {
				
				FacesContext fc = FacesContext.getCurrentInstance();
				String msg = "Lead could not qualify..";
				addGlobalFacesMessage(fc, msg, "saveButton", FacesMessage.SEVERITY_INFO);
				return null;
			}
			
			
			FacesContext fc = FacesContext.getCurrentInstance();
			fc.getExternalContext().getSessionMap().put(ProjectConstant.LEAD_TO_BE_CONVERTED_SESSION_KEY, selectedLeadToConvert);
			
			/* fc.getExternalContext().getRequestMap().remove(ProjectConstant.LEAD_TO_BE_CONVERTED_SESSION_KEY );
			fc.getExternalContext().getRequestMap().put(ProjectConstant.LEAD_TO_BE_CONVERTED_SESSION_KEY,selectedLeadToConvert );*/
			
			log.info("selectedLeadToConvert id ::" + selectedLeadToConvert.getLeadId());
			
			navigationIdentifier =  ProjectConstant.AUTO_CREATE_CONTACT_NAVIGATION_INDENTIFIER;

		} 

		return navigationIdentifier;

		
	}

	
	public String navigateToViewLeads(){

		log.info("inside navigateToViewLeads() ");

		navigationIdentifier= ProjectConstant.VIEW_LEADS_NAVIGATION_IDENTIFIER;
		return navigationIdentifier;

	}

	
	
	


///////////////////////////////////////

	public String getTempVarCLQ() {
		
		log.info("inside getTempVarCLQ()" );

		initForLeadConversion();

		generateUniqueLeadQualificationQuestions();

		tempVarCLQ = "do-nothing-str";
		return tempVarCLQ;
	}

	public void setTempVarCLQ(String tempVarCLQ) {
		this.tempVarCLQ = tempVarCLQ;
	}




	public Lead getSelectedLeadToConvert() {
		return selectedLeadToConvert;
	}



	public void setSelectedLeadToConvert(Lead selectedLeadToConvert) {
		this.selectedLeadToConvert = selectedLeadToConvert;
	}




	public HtmlDataTable getLeadQualificationBODataTable() {
		return leadQualificationBODataTable;
	}



	public void setLeadQualificationBODataTable(
			HtmlDataTable leadQualificationBODataTable) {
		this.leadQualificationBODataTable = leadQualificationBODataTable;
	}



	public HtmlDataTable getRadioValueDataTable() {
		return radioValueDataTable;
	}



	public void setRadioValueDataTable(HtmlDataTable radioValueDataTable) {
		this.radioValueDataTable = radioValueDataTable;
	}


	public Boolean getSaveButtonIsDisabled() {
		return saveButtonIsDisabled;
	}


	public void setSaveButtonIsDisabled(Boolean saveButtonIsDisabled) {
		this.saveButtonIsDisabled = saveButtonIsDisabled;
	}


	public List<LeadQualificationPropertyValueBO> getLeadQualificationPropertyValueBOList() {
		return leadQualificationPropertyValueBOList;
	}


	public void setLeadQualificationPropertyValueBOList(
			List<LeadQualificationPropertyValueBO> leadQualificationPropertyValueBOList) {
		this.leadQualificationPropertyValueBOList = leadQualificationPropertyValueBOList;
	}


	public ILeadManagement getLeadManagementImpl() {
		return leadManagementImpl;
	}


	public void setLeadManagementImpl(ILeadManagement leadManagementImpl) {
		this.leadManagementImpl = leadManagementImpl;
	}

	public IBusinessRuleBL getBusinessRuleImpl() {
		return businessRuleImpl;
	}

	public void setBusinessRuleImpl(IBusinessRuleBL businessRuleImpl) {
		this.businessRuleImpl = businessRuleImpl;
	}


	public Lead getLead() {
		return lead;
	}

	public void setLead(Lead lead) {
		this.lead = lead;
	}




	/*public Integer getLeadId() {
		return leadId;
	}

	public void setLeadId(Integer leadId) {
		this.leadId = leadId;
	}

	public Integer getSelectedLeadIdToConvert() {
		return selectedLeadIdToConvert;
	}



	public void setSelectedLeadIdToConvert(Integer selectedLeadIdToConvert) {
		this.selectedLeadIdToConvert = selectedLeadIdToConvert;
	}*/






}
